package web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import domain.AjaxResult;
import domain.Archive;
import domain.Page;
import service.ArchiveService;

@Controller
@RequestMapping("/archive")
public class ArchiveController {
	@Autowired
	ArchiveService service;
	
	@RequestMapping("/page")
	@ResponseBody
	public Page<Archive> findPage(
			@RequestParam(name = "page", defaultValue = "1") Integer page, 
			@RequestParam(name = "size", defaultValue = "10") Integer size) {
		if (page <= 0) page = 1;
		if (size <= 0) size = 10;
		
		return service.findPage(page, size);
	}
	
	@RequestMapping(path = "/delete", method = RequestMethod.DELETE)
	@ResponseBody
	public AjaxResult delete(@RequestParam("id") Integer id) {
		service.delete(id);
		
		return AjaxResult.success();
	}
	
	@RequestMapping(path = "/update", method = RequestMethod.PUT)
	@ResponseBody
	public AjaxResult update(Archive archive) {
		service.udpate(archive);
		
		return AjaxResult.success();
	}
	@RequestMapping("/add")
	@ResponseBody
	public AjaxResult insert(Archive archive) {
		service.insert(archive);
		return AjaxResult.success();
	}
	@RequestMapping("/delBatch")
	@ResponseBody
	public AjaxResult deleteBatch(@RequestParam("ids")Integer[] ids) {
		if(ids==null||ids.length==0) 
			return AjaxResult.faile();
		
		boolean flag=service.delBatch(ids);
		if(flag) {
			return AjaxResult.success();
		}else {
			return AjaxResult.faile();
		}
	}
}
